﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class UserDAO
    {
        public static bool InsertUser(UserDTO user){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@UserID", user.ID));
                sqlParams.Add(new SqlParameter("@UserPassword", user.Password));
                sqlParams.Add(new SqlParameter("@VaiTro", user.VaiTro));
                sqlParams.Add(new SqlParameter("@Quyen", user.Quyen));
                if (SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertUSER", sqlParams) == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	
            }
            return result;
        }
        public static bool UpdateUser(UserDTO user){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@UserID", user.ID));
                sqlParams.Add(new SqlParameter("@UserPassword", user.Password));
                sqlParams.Add(new SqlParameter("@VaiTro", user.VaiTro));
                sqlParams.Add(new SqlParameter("@Quyen", user.Quyen));
                if (SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateUSER", sqlParams) == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {

            }
            return result;
        }
        public static bool DeleteUser(string userid){

            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@UserID", userid));

                if (SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteUSER", sqlParams) == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {

            }
            return result;
        }
        public static bool CheckUser(string userid){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@UserID", userid));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectUSER", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	
            }
            return result;
        }
        public static DataTable GetUser(string userid)
        {
            DataTable result = null;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@UserID", userid));
                result = SqlDataAccessHelper.ExecuteQuery("usp_SelectUSER", sqlParams);
            }
            catch (System.Exception ex)
            {

            }
            return result;
        }
        public static DataTable GetUserDuoiQuyen(int priority)
        {
            DataTable result = null;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@priority", priority));
                result = SqlDataAccessHelper.ExecuteQuery("usp_SelectUserDuoiQuyen", sqlParams);
            }
            catch (System.Exception ex)
            {

            }
            return result;
        }
    }
}
